﻿@using System.Text.RegularExpressions
@model Easy.Data.Pagination

@{
    string currentUrl = Request.RawUrl.ToLower();
    if (!currentUrl.Contains("p="))
    {
        if (currentUrl.Contains("?"))
        {
            currentUrl += "&p=0";
        }
        else
        {
            currentUrl += "?p=0";
        }
    }
}
<ul class="pagination">
    @if (Model.PageIndex >= 1)
    {
        <li><a href="@Regex.Replace(currentUrl, @"p=\d", "p=" + (Model.PageIndex - 1))">«</a></li>

    }
    else
    {
        <li class="disabled"><a href="javascript:void(0)">«</a></li>
    }
    @{
        int start = Model.PageIndex - 3;
        if (start < 1)
        {
            start = 1;
        }
        else
        {
            start++;
            <li>
                <a href="@Regex.Replace(currentUrl, @"p=\d", "p=" + (start-2))">...</a>
            </li>
        }
        int max = 0;
        for (; start <= Model.AllPage && max < 5; start++)
        {
            <li class="@(start - 1 == Model.PageIndex ? "active" : "")">
                <a href="@Regex.Replace(currentUrl, @"p=\d", "p=" + (start - 1))">@start</a>
            </li>
            max++;
        }
        if (start < Model.AllPage)
        {
            <li>                
                <a href="@Regex.Replace(currentUrl, @"p=\d", "p=" + (start-1))">...</a>
            </li>
        }
    }

    @if (Model.PageIndex < Model.AllPage - 1)
    {
        <li>
            <a href="@Regex.Replace(currentUrl, @"p=\d", "p=" + (Model.PageIndex + 1))">»</a>
        </li>
    }
    else
    {
        <li class="disabled">
            <a href="javascript:void(0)">»</a>
        </li>
    }
</ul>
